package com.bckj.fastboot.iot.parse.controller;

import com.bckj.fastboot.core.extra.cache.CacheOps;
import com.bckj.fastboot.iot.common.constant.IotParseCacheConstants;
import lombok.RequiredArgsConstructor;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * IotParseController
 *
 * @author wfc
 **/
@RestController
@RequiredArgsConstructor
@RequestMapping("iotParse")
public class IotParseController {

    private final CacheOps cacheOps;

    /**
     * 清理协议缓存
     */
    @RequestMapping("clearProtocolCache")
    public boolean clearProtocolCache(String cacheKey) {
        var cache = cacheOps.localCache(IotParseCacheConstants.IOT_PROTOCOL_CACHE_NAME);
        if (StringUtils.hasText(cacheKey)) {
            cache.evict(cacheKey);
        } else {
            cache.clear();
        }
        return true;
    }

    /**
     * 清理报警规则缓存
     */
    @RequestMapping("clearAlarmRuleCache")
    public boolean clearAlarmRuleCache(String cacheKey) {
        var cache = cacheOps.localCache(IotParseCacheConstants.IOT_ALARM_RULE_CACHE_NAME);
        if (StringUtils.hasText(cacheKey)) {
            cache.evict(cacheKey);
        } else {
            cache.clear();
        }
        return true;
    }
}
